const TopicModel = require('../model').topicModel;
const DaoTool = require('../lib/daoTool');
exports.createTopic = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "insert into topic set ?";
        conn.query(sql, [DaoTool.generateModelObject(params, TopicModel, 'create')], function (err, result) {
            printLog("=createTopic=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.addTopicComment = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "update topic set TopicCommentNum=TopicCommentNum+1 where TopicId=?;";
        conn.query(sql, [params.ToTopic], function (err, result) {
            printLog("=addTopicComment=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.reduceTopicComment = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "update topic set TopicCommentNum=TopicCommentNum-1 where TopicId=?;";
        conn.query(sql, [params.ToTopic], function (err, result) {
            printLog("=reduceTopicComment=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.addTopicPraise = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "update topic set TopicPraise=TopicPraise+1 where TopicId=?;";
        conn.query(sql, [params.ToTopic], function (err, result) {
            printLog("=addTopicPraise=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.reduceTopicPraise = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "update topic set TopicPraise=TopicPraise-1 where TopicId=?;";
        conn.query(sql, [params.ToTopic], function (err, result) {
            printLog("=reduceTopicPraise=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.selectTopic = function (conn, params, isCount) {
    return new Promise(function (resolve, reject) {
        let sql = "FROM topic AS tpc LEFT JOIN user AS u ON u.openid = tpc.openid LEFT JOIN praise AS p ON p.openid = ? AND p.ToTopic = tpc.TopicId LEFT JOIN friend AS frd ON frd.openid = ? AND frd.FriendOpenId = tpc.openid WHERE " + formatMatchingSqlStr(params);
        let selectSql = "select tpc.*,p.PraiseId,u.NickName,u.HeadImgUrl " + sql + " order by tpc.TopicId desc " + DaoTool.getPageParamsSqlStr(params) + ";";
        let countSql = "select count(1) as num " + sql;
        conn.query(selectSql, [params.openid, params.openid], function (err, selectResult) {
            printLog("=selectTopic err=", err);
            printLog("=selectTopic=", this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql, [params.openid, params.openid], function (err, countResult) {
                printLog("=selectTopic err=", err);
                printLog("=selectTopic=", this.sql);
                if (err) reject(err);
                resolve({data: selectResult, count: countResult[0].num});
            });
        });
    });
};
exports.selectTopic_noLogin = function (conn, params, isCount) {
    return new Promise(function (resolve, reject) {
        let sql = "FROM topic AS tpc LEFT JOIN user AS u ON u.openid = tpc.openid where tpc.TopicType='PUBLIC' ";
        let selectSql = "select tpc.*,u.NickName,u.HeadImgUrl " + sql + " order by tpc.TopicId desc " + DaoTool.getPageParamsSqlStr(params) + ";";
        let countSql = "select count(1) as num " + sql;
        conn.query(selectSql, [], function (err, selectResult) {
            printLog("=selectTopic err=", err);
            printLog("=selectTopic=", this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql, [], function (err, countResult) {
                printLog("=selectTopic err=", err);
                printLog("=selectTopic=", this.sql);
                if (err) reject(err);
                resolve({data: selectResult, count: countResult[0].num});
            });
        });
    });
};
exports.deleteTopic = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query('delete from topic where openid=? and TopicId=?;',[params.openid,params.TopicId],function (err,result) {
            if(err) {
                reject(err)
            }else{
                resolve('删除成功')
            }
        })
    });
};
exports.pcDeleteTopic = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query('delete from topic where TopicId=?;', [params.TopicId], function (err, result) {
            printLog("=pcDeleteTopic=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.pcTopicList = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "FROM topic AS tpc LEFT JOIN user AS u ON u.openid = tpc.openid " + formatPCMatchingSqlStr(params);
        let selectSql = "select tpc.*,u.NickName,u.UserNo " + sql + " order by tpc.TopicId desc " + DaoTool.getPageParamsSqlStr(params) + ";";
        let countSql = "select count(1) as num " + sql;
        conn.query(selectSql, [], function (err, selectResult) {
            printLog("=pcTopicList err=", err);
            printLog("=pcTopicList=", this.sql);
            if (err) reject(err);
            conn.query(countSql, [], function (err, countResult) {
                printLog("=pcTopicList err=", err);
                printLog("=pcTopicList=", this.sql);
                if (err) reject(err);
                resolve({data: selectResult, count: countResult[0].num});
            });
        });
    });
};
function formatPCMatchingSqlStr(params) {
    let sql = "";
    sql += params.NickName ? ' and u.NickName like ' + mysqlHelper.escape('%' + params.NickName + '%') : "";
    sql += params.UserNo ? " and u.UserNo like " + mysqlHelper.escape('%' + params.UserNo + '%') : "";
    sql += params.TopicContent ? " and tpc.TopicContent like " + mysqlHelper.escape('%' + params.TopicContent + '%') : "";
    sql += params.Province ? " and u.Province = " + params.Province : "";
    sql += params.City ? " and u.City = " + params.City : "";
    if (sql != '')
        sql = ' where ' + sql.substring(4);
    return sql;
}
function formatMatchingSqlStr(params) {
    let sql = "";
    if (!params.openid) {
        if (!params.TopicOwnOpenId) {
            sql += " ((u.Province = '" + params.Province + "' AND u.City = '" + params.City + "') or frd.FriendId IS NOT NULL) ";
            sql += " and tpc.TopicType='PUBLIC' ";
        } else {
            sql += "tpc.TopicOwnOpenId='" + params.TopicOwnOpenId + "'";
            sql += " and tpc.TopicType='PUBLIC' ";
        }
    } else if (!params.TopicOwnOpenId) {
        sql += " ((u.Province = '" + params.Province + "' AND u.City = '" + params.City + "') or frd.FriendId IS NOT NULL) ";
        sql += " and (tpc.TopicType='PUBLIC' or (tpc.TopicType='TO_SELF' and tpc.openid='" + params.openid + "')  or (tpc.TopicOwnOpenId= '" + params.openid + "' or (tpc.TopicType='TO_FRIEND' and tpc.openid in(select frd.FriendOpenId from friend as frd where frd.openid = '" + params.openid + "')))  or (tpc.TopicType='TO_STRANGERS' and tpc.openid not in(select frd.FriendOpenId from friend as frd where frd.openid = '" + params.openid + "'))) and (tpc.openid not in(select BlackOpenId from black_list where black_list.openid= '" + params.openid + "')) ";
    } else {
        sql += "tpc.TopicOwnOpenId='" + params.TopicOwnOpenId + "'";
        sql += " and (tpc.TopicType='PUBLIC' or (tpc.TopicType='TO_SELF' and tpc.openid='" + params.openid + "')  or (tpc.TopicOwnOpenId= '" + params.openid + "' or (tpc.TopicType='TO_FRIEND' and tpc.openid in(select frd.FriendOpenId from friend as frd where frd.openid = '" + params.openid + "')))  or (tpc.TopicType='TO_STRANGERS' and tpc.openid not in(select frd.FriendOpenId from friend as frd where frd.openid = '" + params.openid + "'))) and (tpc.openid not in(select BlackOpenId from black_list where black_list.openid= '" + params.openid + "')) ";
    }
    return sql;
}

